<?php

namespace Encore\MediaManagerSelect;

use Encore\Admin\Form\Field;
use Encore\Admin\Media\MediaManager;
use Illuminate\Support\Arr;

class MediaManagerSelect extends Field
{
    /**
     * @var string
     */
    protected $view = 'media-manager-select::select';

    /**
     * @var array
     */
    protected static $js = [
    ];

    /**
     * @var array
     */
    protected $column = [];

    /**
     * @var array
     */
    protected $placeholder = [];

    /**
     * Distpicker constructor.
     *
     * @param array $column
     * @param array $arguments
     */
    public function __construct($column, $arguments)
    {
        $this->noPJax();//禁用pjax
        $this->column['selected_urls'] = $column;
        $this->label                   = empty($arguments) ? '选择文件' : current($arguments);
        $this->id                      = $this->formatId($column);
    }

    public function getValidator(array $input)
    {
        if ($this->validator) {
            return $this->validator->call($this, $input);
        }

        $rules = $attributes = [];

        if (!$fieldRules = $this->getRules()) {
            return false;
        }

        foreach ($this->column as $key => $column) {
            if (!Arr::has($input, $column)) {
                continue;
            }
            $input[$column]      = Arr::get($input, $column);
            $rules[$column]      = $fieldRules;
            $attributes[$column] = $this->label . "[$column]";
        }

        return \validator($input, $rules, $this->getValidationMessages(), $attributes);
    }

    /**
     * {@inheritdoc}
     */
    public function render()
    {

        //        $selected_urls = old($this->column['selected_urls'], Arr::get($this->value(), 'selected_urls')) ?: [];
        $manager = new MediaManager("/");
        $default = Arr::get($this->value(), 'selected_urls');
        $old     = old($this->column['selected_urls'], $default) ?: [];
        if ($old) {
            !is_array($old) && $old = json_decode($default, true);
            $items = [];
            foreach ($old as $i) {
                !is_array($i) && $i = json_decode($i, true);
                $items[] = $i;
            }
            $old = $items;
        }
        $with = [
            'list'                                           => $manager->ls(),
            'nav'                                            => $manager->navigation(),
            'url'                                            => array_merge($manager->urls(), ['list' => route('admin.media.manager.select.list')]),
            "{$this->column['selected_urls']}_selected_urls" => $old,
        ];
        return parent::render()->with($with);
    }

    /*
    * Disable Pjax for current Request
    *
    * @return void
    */
    public function noPJax()
    {
        $request = \Illuminate\Support\Facades\Request::instance();
        if ($request->headers->has("X-PJAX")) {
            $request->headers->set("X-PJAX", false);
        }
    }
}
